package com.blog.boot.webapp.controller;

import com.blog.boot.api.common.RestMsg;
import com.blog.boot.custom.error.CustomException;
import com.blog.boot.custom.error.impl.CustomExcetiponImpl;
import com.blog.boot.webapp.controller.common.BaseController;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : cailong
 * @since : Created in 上午 10:37 2018/12/10 0010
 * @deprecated : 处理web请求的异常拦截
 */
@ControllerAdvice
public class UnifiedControllerAdvice extends BaseController {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public RestMsg myErrorHandler(Exception e) {
        RestMsg msg = restFactory.getMsg();
        msg.error(appendStackTrace(e));
        return msg;
    }

    @ResponseBody
    @ExceptionHandler(value = UnknownAccountException.class)
    public RestMsg myErrorHandler(UnknownAccountException e) {
        RestMsg msg = restFactory.getMsg();
        return msg.error("用户名不存在");
    }
    @ResponseBody
    @ExceptionHandler(value = IncorrectCredentialsException.class)
    public RestMsg myErrorHandler(IncorrectCredentialsException e) {
        RestMsg msg = restFactory.getMsg();
        return msg.error("密码错误");
    }
    @ResponseBody
    @ExceptionHandler(value = LockedAccountException.class)
    public RestMsg myErrorHandler(LockedAccountException e) {
        RestMsg msg = restFactory.getMsg();
        return msg.error("账户被锁定");
    }
    @ResponseBody
    @ExceptionHandler(value = DisabledAccountException.class)
    public RestMsg myErrorHandler(DisabledAccountException e) {
        RestMsg msg = restFactory.getMsg();
        return msg.error("禁用帐户");
    }
    @ResponseBody
    @ExceptionHandler(value = UnauthorizedException.class)
    public RestMsg myErrorHandler(UnauthorizedException e) {
        RestMsg msg = restFactory.getMsg();
        return msg.error("没有权限");
    }
    /**
     * 拦截捕捉自定义异常
     * @param e 错误接口类
     * @return RestMsg
     */
    @ResponseBody
    @ExceptionHandler(value = CustomExcetiponImpl.class)
    public RestMsg myErrorHandler(CustomException e) {
        RestMsg msg = restFactory.getMsg();
        msg.error(appendStackTrace((CustomExcetiponImpl)e)).setCode( StringUtils.isEmpty(e.getErrorCode()) ? "999" : e.getErrorCode() );
        return msg;
    }
    //拼接堆栈信息以及错误行号
    private String appendStackTrace(Exception e){
        StringBuffer buffer = new StringBuffer();
        StackTraceElement stackTraceElement = e.getStackTrace()[0];
        buffer.append( "A file name :")
              .append(stackTraceElement.getFileName())
              .append(" error :'")
              .append(e.getMessage())
              .append("' appears on line ")
              .append(stackTraceElement.getLineNumber());
        return buffer.toString();
    }
}
